#include "SDL.h"
#include "SDL_image.h"
#include "game.h"

// for getenv(const char * name)
#include <stdlib.h>
#include <stdio.h>

/* Enum states
 *	
 */
enum MENU_STATE {
	NEW_GAME,
	LOAD_GAME,
	OPTIONS,
	CREDITS
};

/* 
 * Global variable initialization
 */
	SDL_Surface *surface;
	SDL_Surface *background;
	SDL_Surface *title;
	SDL_Rect *src;
	SDL_Rect *dest;
	const char *cd ;
	
	int status = 0;
	
void A2_SDL_INIT() {
	src = (SDL_Rect *)malloc(sizeof(SDL_Rect));
	dest = (SDL_Rect *)malloc(sizeof(SDL_Rect));
	printf("Initializing SDL. (through A2_SDL_INIT)\n");
	
	/* Initialize defaults, Video and Audio */
	if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
		fprintf(stderr, "Could not initialize SDL: %s.\n", SDL_GetError());
		exit(-1);
	}
	
	/* Set up what happens when the app is manually closed */
	atexit(SDL_Quit);
	
	/* Setting up screen size of 1024x768 with 32-bit depth onto hardware memory */
	surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BIT_DEPTH, SDL_DOUBLEBUF | SDL_HWSURFACE);
	if (surface == NULL) {
		fprintf(stderr, "Couldn't set 1024x768x32 video mode: %s\n", SDL_GetError());
		exit(-1);
	}
	
	// getting present directory so we can always work in relative path
	cd = getenv("PWD");
}

/* assumes relative path from trunk/ 
 *
 *	Always provide input path with "/" not error controlled
 */
SDL_Surface* A2_Load_Image(char* path) {
	char* tmp_cd = (char*)malloc(strlen(cd)+strlen(path));
	strcpy(tmp_cd, cd);
	
	SDL_Surface* to_return = IMG_Load(strcat(tmp_cd, path));
	if (to_return == NULL) {
		fprintf(stderr, "Couldn't load image: %s\n", IMG_GetError());
		exit(-1);
	}
	
	SDL_Surface* temp_pointer = to_return;
	to_return = SDL_DisplayFormatAlpha(to_return);
	free(temp_pointer);
	
	status = SDL_SetAlpha(to_return, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
	if (status != 0) {
		fprintf(stderr, "Couldn't set alpha blending on image: %s\n", SDL_GetError());
	}
	
	free(tmp_cd);
	return to_return;
}

/* Function:		A2_Set_Rect
 * Outputs:			int value indicating success or failure
 *
 * Comments:		Sets an SDL_Rect structure's internal parameters which are:
 *					x - horizontal start location within the picture (in pixels)
 *					y - vertical start location within the picture
 *					w - width from x
 *					h - height from y
 */
int A2_Rect_Set(SDL_Rect* rect, Sint16 x, Sint16 y, Uint16 w, Uint16 h) {
	rect->x = x;
	rect->y = y;
	rect->w = w;
	rect->h = h;
	
	return 0;
}

void A2_Rect_Shift_Up() {
	
}

void A2_Rect_Shift_Down() {
	
}

int main (int argc, char* argv[]) {
	SDL_Event event;
	enum MENU_STATE menu = NEW_GAME;
	
	A2_SDL_INIT();
	
	background = A2_Load_Image("/CResources/titlescreen_background.png");
	title = A2_Load_Image("/CResources/titlename.png");
	
	// Set source and destination frame size and width
	A2_Rect_Set(src, 32, 32, 32, 32);
	A2_Rect_Set(dest, SCREEN_WIDTH/2 - (title->w)/2, (int)(SCREEN_HEIGHT * 0.1), 32, 32);
	
	// Set key repeat rate
	status = SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
	if (status != 0) {
		fprintf(stderr, "Couldn't set up key repeat: %s\n", SDL_GetError());
		exit(-1);
	}
	
	status = SDL_BlitSurface(title, NULL, background, dest);
	if (status != 0) {
		fprintf(stderr, "Couldn't blit: %s\n", SDL_GetError());
		exit(-1);
	}
	
	status = SDL_BlitSurface(background, NULL, surface, NULL);
	if (status != 0) {
		fprintf(stderr, "Couldn't blit: %s\n", SDL_GetError());
		exit(-1);
	}
	
	while(1) {
		
		status = SDL_Flip(surface);
		if (status == -1) {
			fprintf(stderr, "Couldn't flip: %s\n", SDL_GetError());
			exit(-1);
		}
		
		if(SDL_PollEvent(&event)){
			/* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
			switch(event.type){
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym) {
						case SDLK_q:
							SDL_FreeSurface(title);
							SDL_FreeSurface(background);
							SDL_FreeSurface(surface);
							
							/* Shutdown all subsystems */
							SDL_Quit();
							
							free(src);
							free(dest);
							exit(0);
							break;
						case SDLK_UP:
							break;
						case SDLK_DOWN:
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
		}
	}
}